package com.lanou.web.controller;

import com.lanou.entity.Commodity;
import com.lanou.service.CartService;
import com.lanou.utils.ShiHuiResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisPool;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;


@RestController
public class CartController {
    @Autowired
    private CartService cartService;

    @Autowired
    JedisPool jedisPool;
    @Value("${CAT_JSON_URL}")
    private String CAT_JSON_URL;

    @RequestMapping("/cart/checked")//获取所有选中的商品11111111111111111111
    public Map<String, Object> getCheckedCart(Long userId) {
        List<Commodity> commodities = cartService.getCheckedCart(userId);
        return ShiHuiResult.ok(commodities);
    }

    @RequestMapping("/cart")//添加购物车111111111111111111111111111
    public Map<String, Object> editCart(Long itemId, Integer num, Long userId) {
        cartService.editCart(itemId, num, userId);
        return ShiHuiResult.ok();
    }

    @RequestMapping("/cart/status")//修改状态11111111111111111111111111111111111111
    public Map<String, Object> cartStatus(Integer status, Long userId, Long itemId) {
        List<Commodity> commodityList = cartService.amendStatus(status, userId, itemId);
        return ShiHuiResult.ok(commodityList);
    }

    @RequestMapping("/cart/deletes")//删除购物车所有商品111111111111111111111
    public Map<String, Object> cartLoginDeletes(Long userId) {
        cartService.cartDeletes(userId);
        return ShiHuiResult.ok();
    }

    @RequestMapping("/cart/dle")//删除购物车单个商品111111111111111111111
    public Map<String, Object> cartLoginDle(Long userId, Long itemId) {
        cartService.cartDle(userId, itemId);
        return ShiHuiResult.ok();
    }

    @RequestMapping("/cart/statusAll")//全选 状态设置为1 1111111111111111111111111111111111111
    public Map<String, Object> cartStatusAll(Long userId,Integer status) {
        List<Commodity> commodityList = cartService.cartStatusAll(userId,status);
        return ShiHuiResult.ok(commodityList);
    }

    @GetMapping("/cartItems")//通过用户id查询redis中所有商品111111111111111111111111111111111
    public Map<String, Object> cartItems(Long userId) {
        List<Commodity> commodities = cartService.getCartItemList(userId);
        return ShiHuiResult.ok(commodities);
    }

    @RequestMapping("/cart/merge/all")//合并cookie+redis1111111111111111111111111111111
    public Map<String, Object> loginCartAll(Long userId, HttpServletRequest request) {
        List<Commodity> commodityList = cartService.loginMergeAll(userId, request);
        return ShiHuiResult.ok(commodityList);
    }

}

